import {  UploadProps } from "element-plus"
import useUserStore from "@/store/modules/user"
const useStoreUser = useUserStore()

/**
 * 
 * @param callback 上传成功执行的回掉函数
 * 
 */
export const useUploadXlsx =(callback: () => void)=> {
    // 上传文件路径
    // 携带参数
    const headers = {
        token: useStoreUser.token
    }
    // 文件上传成功回掉
    const handleSuccess: UploadProps['onSuccess'] = (
        response,
    ) => {
        if (response.code == 201) {
            ElMessage.success(response.msg)
            if(callback) {
                callback()
            }
            return
        }
        ElMessage.error(response.msg)
    
    }
    // 文件上传类型限制
    const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
        if (!rawFile.name.endsWith('.xlsx') && rawFile.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
            ElMessage.error('上传类型错误,只限于xlsx')
            return false
        } else if (rawFile.size / 1024 / 1024 > 10) {
            ElMessage.error('文件过大删除一点试试')
            return false
        }
        return true
    }

    return {
        headers,
        handleSuccess,
        beforeUpload,
    }
}
